﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
namespace TranTekBE.Missed
{
    public partial class Addmissed : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TranTekBE.Maintenance.func abcd = new TranTekBE.Maintenance.func();
            /*
             * Khởi tạo thông tin cơ bản 
             */
            lastname.Text = UserInfo.LastName;
            employee.Text = UserInfo.FirstName + " " + UserInfo.LastName;
            /*
                 * Lấy danh sách cửa hàng ( gồm id và tên )
                 */
            if (!IsPostBack)
            {
                if(Request.QueryString["add"] == "ok")
                    ok.Text = "<div class=\"alert alert-success\">Thêm đơn hàng nhỡ thành công!</div>";
                DataSet liststore = FunctionsUtils.Database.ExecuteQuery("SELECT C007000 as ID, C007001 as Name FROM T0070");
                if (liststore != null)
                {
                    FunctionsUtils.BindDataForControlList(idstore, liststore, "ID", "Name");
                }
                stock.Checked = true;
           /*
           * Lấy danh sách top 5 hóa đơn nhỡ mới nhất ( id, tiêu đề, nội dung, thời gian... )
           */
                DataSet listmissedtop5 = FunctionsUtils.Database2.ExecuteQuery("SELECT TOP 5 * FROM tbl_Missed ORDER BY ID DESC");
                if (listmissedtop5 != null)
                {
                    int stt = 1;
                    foreach (DataRow Rows in listmissedtop5.Tables[0].Rows)
                    {
                        top5ordermissed.Text += "<tr class=\"odd gradeX\">";
                        top5ordermissed.Text += "<td>" + stt + "</td>";
                        top5ordermissed.Text += "<td>" + Rows[2].ToString() + "</td>";
                        top5ordermissed.Text += "<td>" + Rows[1].ToString() + "</td>";
                        top5ordermissed.Text += "<td>" + abcd.getWhy(Rows[3].ToString()) + "</td>"; //lydo
                        top5ordermissed.Text += "<td>" + abcd.getCuaHang(Rows[5].ToString()) + "</td>";
                        top5ordermissed.Text += "</tr>";
                        stt++;
                    }
                }
            }
 
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]    
        public static List<string> getnameserver(string value)
        {
            List<string> abc = new List<string>();
            DataSet searchlist = FunctionsUtils.Database.ExecuteQuery("SELECT TOP 10 C001203 as ID, C001204 as Name FROM T0012 WHERE C001204 LIKE '%" + value + "%'");
            if (searchlist != null)
            {
                foreach (DataRow Rows in searchlist.Tables[0].Rows)
                {
                    abc.Add(Rows[1].ToString());
                }
            }

            return abc;
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            status.Text = "";
            ok.Text = "";
            /*
             * Info
             */
            string IDthuoc = "-1";
            string Tenthuoc = tbAuto.Text;
            int IDlydo = 1;
            if (noavaible.Checked)
                IDlydo = 2;
            string IDnguoigui = UserInfo.UserName;
            string IDstore = idstore.SelectedValue;
            string ghichu = note.Text;
            string now = DateTime.Now.ToString("MM/dd/yyyy");
            /*
             * Kiểm tra xem thuốc đã có trong datavase hay chưa
             */
            DataSet checkidthuoc = FunctionsUtils.Database.ExecuteQuery("SELECT C001203 as ID, C001204 as Name FROM T0012 WHERE C001204 = '" + Tenthuoc + "'");
            if (checkidthuoc != null)
            {
                foreach (DataRow Rows in checkidthuoc.Tables[0].Rows)
                {
                    IDthuoc = Rows[0].ToString();
                }
            }
            if (Tenthuoc.Length > 0)
            {
                string sql = "INSERT INTO tbl_Missed VALUES (N'" + IDthuoc + "',N'" + Tenthuoc + "'," + IDlydo + ",N'" + IDnguoigui + "',N'" + IDstore + "',N'" + ghichu + "',N'" + now + "')";
                FunctionsUtils.Database2.ExecuteNonQuery(sql);
                Response.Redirect(Request.RawUrl+"?add=ok");
            }
            else
            {
                status.Text = "<font color='red'>Dữ liệu không được để trống</font>";
            }
        }
    }
}